package com.bocai.freeman.view.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseFragment;
import com.bocai.freeman.utils.DividerItemDecoration;
import com.bocai.freeman.view.activitys.DynamicIssueActivity;
import com.bocai.freeman.view.activitys.PerfectLightDetailActivity;
import com.bocai.freeman.view.adapter.PerfectLightAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 人脉Frag
 */
public class ContactFrag extends BaseFragment {


    View view;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.iv_no_network)
    ImageView ivNoNetwork;
    @Bind(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;


    private PerfectLightAdapter mPerfectLightAdapter;
    private List<List<Integer>> mData;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_contact, null);
        ButterKnife.bind(this, view);
        initView();
        initEvent();

        return view;
    }


    @Override
    public void initEvent() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       swipeRefreshLayout.setRefreshing(false);
                    }
                }, 350);
            }
        });
    }

    private void initView() {
        initToolbar();
        mData = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            List<Integer> list = new ArrayList<>();
            for (int j = 0; j < 9 - i; j++) {
                list.add(R.mipmap.img01);
            }
            mData.add(list);
        }
        Log.e("tag", mData.size() + "");
        mPerfectLightAdapter = new PerfectLightAdapter(getActivity(), mData);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.addItemDecoration(new DividerItemDecoration(getActivity(),
                DividerItemDecoration.VERTICAL_LIST));
        rv.setAdapter(mPerfectLightAdapter);
        mPerfectLightAdapter.setOnItemClickLitener(new PerfectLightAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(new Intent(getActivity(),PerfectLightDetailActivity.class));
            }
        });
    }

    private void initToolbar() {
        title.setText("我的人脉圈");
        toolbar.inflateMenu(R.menu.menu_add);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
             startActivity(new Intent(getActivity().getApplicationContext(), DynamicIssueActivity.class));
                return false;
            }
        });
    }

    @Override
    public void setData(Object obj) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
